<?php 
session_start();
$_SESSION['url']= $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
include 'connexion.php';

if (isset($_GET["id"])) 
{
    $id=$_GET["id"];


    $sql_bien = "SELECT * FROM bien where id=".$id;
    $req_bien = mysqli_query($db,$sql_bien) or die('Erreur SQL !'.$sql_bien.'<br>'.mysqli_error($db)); 
 
 

    if (isset($_POST["acheter"])) 
    {
        if (empty($_SESSION['id_user'])) {
            echo '  
                    <script>
                        alert("Vous n\'êtes pas coonnecter ou pas inscrit vous ne pouvez donc pas louer ce bien. Vous allez donc être redirigé(e) vers la page de connection.");
                        document.location.href = \'login.php\';
                    </script>

                 ';

        }
        else
        {
            $sql_acheter_bien = "UPDATE reservation SET confirmation=1 where id_bien=".$id;
            $req_acheter_bien = mysqli_query($db,$sql_acheter_bien) or die('Erreur SQL !'.$sql_acheter_bien.'<br>'.mysqli_error($db));
            $sql_acheter_bien1 = "UPDATE reservation SET id_acheteur=".$_SESSION['id_user']." where id_bien=".$id;
            $req_acheter_bien1 = mysqli_query($db,$sql_acheter_bien1) or die('Erreur SQL !'.$sql_acheter_bien1.'<br>'.mysqli_error($db));
        }
        
        
    }

}
?> 

<html>
    <head>
          <link rel="stylesheet" type="text/css" href="style.css"/>
      <meta charset="UTF-8" />
      <meta name="description" content="Free Bed ou comment trouver un endroit pour dormir en 2 cliques."/>
      <title>Free Bed</title>
    </head>
    <body>
      <div id="conteneur">
        <script type="text/javascript">

            alert("<?php echo $erreur ?>");

        </script>   
            <div id="header">
                <?php include_once("header.php"); ?>
        </div><br/>
            <div id="contenu">
                <?php 
                        while ( $data= mysqli_fetch_array($req_bien))
                          { 

                                $sql_type = "SELECT type FROM type where id=".$data['id_type'];
                                $res_type = mysqli_query($db,$sql_type) or die('Erreur SQL !'.$sql_type.'<br>'.mysqli_error($db)); 
                                $data1 = mysqli_fetch_array($res_type);

                                $sql_tarif = "SELECT * FROM tarif where id=".$data['id_tarif'];
                                $res_tarif = mysqli_query($db,$sql_tarif) or die('Erreur SQL !'.$sql_tarif.'<br>'.mysqli_error($db));
                                $data2 = mysqli_fetch_array($res_tarif);

                                $sql_pays = "SELECT pays FROM pays where id=".$data['id_pays'];
                                $res_pays = mysqli_query($db,$sql_pays) or die('Erreur SQL !'.$sql_pays.'<br>'.mysqli_error($db)); 
                                $data3 = mysqli_fetch_array($res_pays);

                                $sql_user = "SELECT nom FROM user where id=".$data['id_user'];
                                $res_user = mysqli_query($db,$sql_user) or die('Erreur SQL !'.$sql_user.'<br>'.mysqli_error($db));
                                $data4 = mysqli_fetch_array($res_user);

                                $sql_photo = "SELECT photo FROM photo where id_bien=".$data['id'];
                                $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                $data5 = mysqli_fetch_array($res_photo);

                                if (mysqli_num_rows($res_photo) == 0) {
                                    $sql_photo = "SELECT photo FROM photo where id_bien=0";
                                    $res_photo = mysqli_query($db,$sql_photo) or die('Erreur SQL !'.$sql_photo.'<br>'.mysqli_error($db));
                                    $data5 = mysqli_fetch_array($res_photo);
                                }

                                $sql_date = "SELECT * FROM reservation where id_bien=".$data['id'];
                                $res_date = mysqli_query($db,$sql_date) or die('Erreur SQL !'.$sql_date.'<br>'.mysqli_error($db));
                                $data6 = mysqli_fetch_array($res_date);

                                if ($data6['confirmation']==1) 
                                {
                                    $_SESSION['louer'] = "Ce bien est louée";
                                }
                                else
                                {
                                    $_SESSION['louer'] = "Ce bien n'est pas louée";
                                }

                                $sql_ville = "SELECT ville FROM ville where id=".$data['id_ville'];
                                $res_ville = mysqli_query($db,$sql_ville) or die('Erreur SQL !'.$sql_ville.'<br>'.mysqli_error($db)); 
                                $data7 = mysqli_fetch_array($res_ville);


                                echo "  <form method='post'>
                                            <table border='2px'>
                                                <tr>
                                                    <td rowspan=7>
                                                        <img src=".$data5['photo'].">
                                                    </td>
                                                    <td colspan=2>
                                                        Description : ".utf8_encode(stripslashes($data['description']))."
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Adresse : ".$data['adresse'].",".$data7['ville'].", ".$data3['pays']."
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Type : ".$data1['type']."
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Tarif à la journée : ".$data2['tarif_jour']." €
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Tarif à la semaine : ".$data2['tarif_semaine']." €
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Louable à partir de : ".$data6['date_debut']." à ".$data6['date_fin']."
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan=2>
                                                        Surface : ".$data['surface']." m²
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Mise en ligne par : ".$data4['nom']."
                                                    </td>
                                                    <td>
                                                        ".$_SESSION['louer']."
                                                    </td>
                                                    <td>
                                                        Pour louer ce bien veuillez cilquer <input type='submit' id='acheter' name='acheter' value='ICI'/>
                                                    </td>
                                                </tr>
                                            </table>
                                        </form>";
                          }
                ?>
            </div>
            <div id="footer">
                <?php include_once("footer.html"); ?>

            </div>

      </div>
    </body>
</html>
